package com.example.myactspringboot.controller;

import com.example.myactspringboot.bean.PageVo;
import com.example.myactspringboot.bean.SysRole;
import com.example.myactspringboot.server.RoleService;
import com.example.myactspringboot.util.R2;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import java.util.List;

/**
 * @Create 2023-09-24 13:47
 * @Description
 */
@Controller
@RequestMapping("role")
public class RoleManageController {
    @Autowired
    RoleService service;

    @GetMapping("/toRoleMangePage")
    public String toRoleMangePage() {
        return "roleManage";
    }

    @GetMapping("/getAllRole/{page}/{rows}")
    @ResponseBody
    public R2 getAllRole(@PathVariable int rows, @PathVariable int page) {
        PageHelper.startPage(page, rows);
        List<SysRole> allRole = service.getAllRole();
        //数据，连续显示的页数
        PageInfo<SysRole> pageInfo = new PageInfo<>(allRole, 5);
        return R2.ok().data(pageInfo);
    }
    @PostMapping("/getAllRole1")
    @ResponseBody
    public R2 getAllRole2(@RequestBody PageVo page) {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<SysRole> allRole = service.getAllRole();
        //数据，连续显示的页数
        PageInfo<SysRole> pageInfo = new PageInfo<>(allRole, 5);
        return R2.ok().data(pageInfo);
    }

    @PostMapping("delRole/{roleId}")
    @ResponseBody
    public R2 delRole(@PathVariable String roleId) {
        int i = service.delRelevanceRole(roleId);
        return i > 0 ? R2.ok().message("删除成功") : R2.error().message("删除失败");
    }


    @GetMapping("/toRoleMenuBind/{roleId}")
    public String toRoleMenuBind(@PathVariable String roleId, Model model) {
        model.addAttribute("roleId",roleId);
        return "roleMenuBind";
    }

    @GetMapping("/getById/{roleId}")
    @ResponseBody
    public R2 getRoleById(@PathVariable String roleId){
        SysRole sysRole = service.getById(roleId);
        return R2.ok().data(sysRole);
    }

}
